import Taro from '@tarojs/taro'
import app from '../../main.js'
let myJs = {
    getData: function() {
        this.state = {
            editable: true,
            isOpened: false,
            swiperNum: 1,
            images: [],
            inspect_data: {},
            inspect_list: [],
            displays: {},
            get_array:[],
            case_courses: [
               
            ],
            case_detail: {
                id: 1,
                status: '',
                patient_name: '张三',
                gender: '男',
                birthday: '1978-2-13',
                phone: '13456543212',
                province: '广东',
                city: '广州',
                code: '784321344',
                visit_at: '', //就诊日期
                main_suit: '', //主诉
                past_illness: '', //既往史
                present_illness:'', //现病史
                family_illness:'', //家族史
                zhenduan:'',//诊断
                zhiliao:'', //治疗
                physical_images: [], //照片
                inspect_data: {},//检查
            },
            template: [],
            tempImg: '',
            text: '',
            imgModal: false
        };
        return this.state;
    },

    imageChange() {
        var me = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['original'],
            sourceType: ['album', 'camera'],
            success(res) {
                const tempFilePaths = res.tempFilePaths;
                me.state.tempImg = tempFilePaths[tempFilePaths.length - 1];
                me.setState({
                    imgModal: true,
                    text: ''
                });
            }
        })
    },
    deleteImage(url) {
        var me = this;
        Taro.showModal({
            title: '提示',
            content: '是否确定删除该图片',
            success: function(res) {
                if (res.confirm) {
                    var case_detail = me.state.case_detail;
                    var list = case_detail.physical_images;
                    var result = [];
                    for (var i = 0; i < list.length; i++) {
                        if (list[i].url != url) {
                            result.push(list[i]);
                        }
                    }
                    case_detail.physical_images = result;
                    me.setState({
                        case_detail: case_detail,
                    });
                    me.uploadPhysicalImages(case_detail.id, result);
                }
            }
        })
        
    },
    closeModal() {
        this.setState({
            imgModal: false,
            tempImg: ''
        });
    },
    comfirmModal() {
        var me = this;
        if(!this.state.text) {
            Taro.showToast({
                title: '请输入照片名称',
                icon: 'none',
                duration: 2000
            })
            return;
        }

        Taro.showLoading({
            title: '上传中...',
            mask: true
        })

        wx.uploadFile({
            url: app.apiUri + '/admin/upload/image',
            filePath: me.state.tempImg,
            name: 'file',
            formData: {
              name: 'file'
            },
            success(res) {
              Taro.hideLoading();
              let data = JSON.parse(res.data);
              if (data.status == 200) {
                var case_detail = me.state.case_detail;
                case_detail.physical_images.push({url: app.getPath(data.result), text: me.state.text});
                me.setState({
                    case_detail: case_detail,
                    imgModal: false,
                    tempImg: ''
                });
              }
              me.uploadPhysicalImages(me.state.case_detail.id, case_detail.physical_images);
            }
          })
    },
    uploadPhysicalImages(id, physical_images) {
        var me = this;
        app.post('api/case_details/uploadPhysicalImages', {id: id, physical_images: JSON.stringify(physical_images)}, function(res) {
            // var case_detail = me.state.case_detail;
            // case_detail.physical_images = res.data.physical_images;
            // me.setState({
            //     case_detail: case_detail
            // });
        });
    },

    submit() {
        var me = this;
        if(me.state.editable) {
            app.post("api/case_details/updateState", {id: this.state.case_detail.id}, function(res) {
                me.showToast('提交成功');
                me.setState({
                    editable: false
                });
            });
        }
    },

    delete() {
        var me = this;
        app.isConfirm('是否确定删除该病历', function() {
            app.post("api/case_details/delete", {id: me.state.case_detail.id}, function(res) {
                let status = res.data.status;
                if (status == 200) {
                    Taro.showToast({
                        title: '删除成功',
                        icon: 'success',
                        duration: 2000,
                        complete: function() {
                            app.navigateBack('../medicalhomepage/medicalhomepage');
                        }
                    });
                } else {
                    Taro.showToast({
                        title: '删除失败',
                        icon: 'none',
                        duration: 200
                    });
                }
            });
        });
    },

    getCaseTemplate(name) {
        var me = this;
        app.get("api/case_templates/getByName", {name: name}, function(res) {
            if(res.data.result && res.data.result.displays) {
                var displays = {};
                var temp = res.data.result.displays.split(',');
                for(var i = 0; i < temp.length; i++) {
                    displays[temp[i]] = true;
                }
                me.setState({
                    displays: displays
                });
            }
        });
    },

    getBaseTemplate() {
        var me = this;
        let data = {id:1};
        app.get("web/case/base_template", data, function(res) {
            console.log('tem',res)
            me.setState({
                template: res.data.result
            });
        });
    },

    getCase_detail() {
        var me = this;
        app.get("api/case_details/get", {id: me.state.case_detail.id}, function(res) {
            var data = res.data.result;
            data.main_suit = !app.isEmpty(data.main_suit) ? data.main_suit : '';
            data.present_illness = !app.isEmpty(data.present_illness) ? data.present_illness : '';
            data.jiwang_illness = !app.isEmpty(data.jiwang_illness) ? data.jiwang_illness : '';
            data.chuanranbingshi = !app.isEmpty(data.chuanranbingshi) ? data.chuanranbingshi : '';
            data.guominshi = !app.isEmpty(data.guominshi) ? data.guominshi : '';
            data.family_illness = !app.isEmpty(data.family_illness) ? data.family_illness : '';
            data.diagnosis = !app.isEmpty(data.family_illness) ? data.diagnosis : '';
            data.cure = !app.isEmpty(data.cure) ? data.cure : '';
            data.visit_at = !app.isEmpty(data.visit_at) ? data.visit_at : '';
            data.department = !app.isEmpty(data.department) ? data.department : '';
            data.patient_name = data.patient_name.substr(0, 1) + "**";

            if(data.physical_images && data.physical_images != 'null') {
                data.physical_images = JSON.parse(data.physical_images);
            } else {
                data.physical_images = [];
            }

            me.state.swiperNum = data.physical_images.length > 1? 2: 1;

            var inspect_data = {};

            if(data.inspect_data) {
                inspect_data = JSON.parse(data.inspect_data);
            }

            var editable = false;

            if (data.type == '病例') {
                if (data.status == '待提交' || data.status == '初审待修改' || data.status == '终审待修改')
                    editable = true;
            } else {
                editable = true;
            }

            me.setState({
                case_detail: data,
                inspect_data: inspect_data,
                editable: editable,
                inspect_list: me.getInspectList(inspect_data, me.state.template)
            });
        });
    },
    getInspectList(inspect_data, template) {
        var inspect_template = {};
        for (var i = 0; i < template.length; i++) {
            if (template[i].type == '眼科检查') {
                inspect_template = template[i]['displays'];
                break;
            }
        }
        var inspect_list = [];
        for (var key in inspect_template) {
            var item = {label: key, v1: '', v2: ''};
            item.v1 = this.getEyeValue(key, inspect_data, 'v1');
            item.v2 = this.getEyeValue(key, inspect_data, 'v2');
            inspect_list.push(item);
        }
        return inspect_list;
    },
    getEyeValue(key, inspect_data, eye) {
        if (!inspect_data) {
            return '';
        }
        var obj = inspect_data[eye];
        if (!obj) {
            return '';
        }
        var one = obj[key];
        if (key == '矫正视力') {
            return one.v1 + 'DS/' + one.v2 + 'DCX' + one.v3 + '->' + one.v4;
        } else {
            return one.v1;
        }
    },
    allCourse() {
        var me = this;
        app.get("api/case_details/allCourse", {case_id: me.state.case_detail.id}, function(res) {
            console.log('res',res)
            me.setState({
                case_courses: res.data.result
            });
        });
    },
    deleteCourse(id) {
        var me = this;
        app.post("api/case_details/deleteCourse", {id: id}, function(res) {
            me.allCourse();
        });
    },
    navigateTo(url) {
        app.navigateTo(url);
    },
    handleChange(name, v) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        
        temp[names[i]] = v;
        this.setState({n: temp});
    },
    redirectTo(url) {
        app.redirectTo(url);
    },

    navigateBack(url) {
        app.navigateBack(url);
    },
    reLaunch(url) {
        app.reLaunch(url);
    },
    componentDidMount () {
    },
    componentWillUnmount () {
    },
    componentDidHide () {}
}
export default myJs;
